import { ipcMain } from 'electron';

import { log } from '../logger';
import { HttpServer } from './httpServer';
import { IpcServer } from './ipcServer';

// --- (未来可以添加 HttpServer 类，也可以提取到单独文件) ---
// class HttpServer { ... }

// --- 统一的服务器模块初始化函数 ---
export function initializeServerModules(): void {
  log.info('[ServerCore] Initializing server modules...');
  try {
    // 实例化导入的 IPC 服务器
    new IpcServer(ipcMain);
    log.info('[ServerCore] IpcServer instance created.');

    // 根据环境变量决定是否启动 HTTP 服务器
    const enableHttp = process.env.ENABLE_HTTP_SERVER?.toLowerCase() === 'true';
    log.info(
      `[ServerCore] Checking for HTTP Server enablement (ENABLE_HTTP_SERVER=${process.env.ENABLE_HTTP_SERVER}). Enabled: ${enableHttp}`
    );

    if (enableHttp) {
      new HttpServer();
      log.info('[ServerCore] HttpServer instance created.');
    } else {
      log.info('[ServerCore] HTTP Server is disabled based on configuration.');
    }

    log.info('[ServerCore] Server modules initialized successfully.');
  } catch (error) {
    log.error('[ServerCore] Failed to initialize server modules:', error);
  }
}
